@extends('layouts.admin.application')

@section('content')
    <div class="main-content">
        <div class="col-xs-12">
            <div class="breadcrumbs" id="breadcrumbs">
                <ul class="breadcrumb">
                    <li><i class="icon-home home-icon"></i><a href="#">首页</a></li>
                    <li>采购进货</li>
                </ul>
            </div>

            @include('layouts.partials._flash')

            <div class="table-responsive">
                <div id="sample-table-2_wrapper" class="dataTables_wrapper" role="grid">
                    <div class="row">
                        <div class="col-sm-2">
                            <div id="sample-table-2_length" class="dataTables_length">
                                <a href="{{ route('cashier.purchase.create') }}"
                                   class="btn btn-sm btn-primary">商品录入</a>
                                <a href="javascript:void(0);" class="btn btn-sm btn-purple del_all">删除</a>
                            </div>
                        </div>

                        <form>
                            <div class="col-sm-10">
                                <div class="dataTables_filter" id="sample-table-2_filter">
                                    <label>商品名称: <input type="text" name="key" value="{{Request::input('key')}}"
                                                        aria-controls="sample-table-2"
                                                        placeholder="按商品名"></label>
                                    <label>
                                        <select class="dataTables_filter" name="category_id" style="width: 180px;">
                                            <option value="-1">所有分类</option>
                                            @foreach ($filter_categories as $category)
                                                <option value="{{$category->id}}">
                                                {{$category->name}}
                                                @foreach($category->children as $child)
                                                    <option value="{{$child->id}}"
                                                            @if($child->id == Request::input('category_id')) selected @endif>
                                                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{$child->name}}
                                                    </option>
                                                    @endforeach
                                                    </option>
                                                @endforeach
                                        </select>
                                    </label>
                                    <label>
                                        <select class="dataTables_filter" name="brand_id" style="width: 180px;">
                                            <option value="-1">所有品牌</option>
                                            @foreach ($brands as $brand)
                                                <option value="{{ $brand->id }}"
                                                        @if($brand->id == Request::input('brand_id')) selected @endif>
                                                    {{ $brand->name }}
                                                </option>
                                            @endforeach
                                        </select>
                                    </label>
                                    <label>
                                        <button type="submit" class="btn btn-xs btn-info" id="search">确定</button>
                                        <a href="{{route('cashier.purchase.index')}}"
                                           class="btn btn-xs btn-default">重置</a>
                                    </label>
                                </div>
                            </div>
                        </form>
                    </div>
                    <table id="sample-table-2"
                           class="table table-striped table-bordered table-hover dataTable del-table"
                           aria-describedby="sample-table-2_info">
                        <thead>
                        <tr role="row">
                            <th class="center sorting_disabled" role="columnheader" rowspan="1" colspan="1"
                                aria-label="">
                                <label>
                                    <input type="checkbox" class="ace" id="check_all">
                                    <span class="lbl"></span>
                                </label>
                            </th>
                            <th>编号</th>
                            <th>商品条码</th>
                            <th>名称</th>
                            <th>所属门店</th>
                            <th>图片</th>
                            <th>规格</th>
                            <th>单位</th>
                            <th>类别</th>
                            <th>品牌</th>
                            <th>零售价</th>
                            <th>会员价</th>
                            <th>进价</th>
                            <th>库存</th>
                            <th>有效期</th>
                            <th>排序</th>
                            <th>生产厂家</th>
                            <th>录入人</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>

                        @foreach($purchases as $product)
                            <tr class="odd" data-id="{{$product->id}}">
                                <td class="center">
                                    <label>
                                        <input type="checkbox" class="ace checked_id" name="checked_id[]"
                                               value="{{$product->id}}">
                                        <span class="lbl"></span>
                                    </label>
                                </td>
                                <td>{{$product->id}}</td>
                                <td>{{$product->code}}</td>
                                <td>{{$product->name}}</td>
                                <td>{{$product->store['name']}}</td>
                                <td><img src="{{$product->image}}" style="width: 40px;height: 35px;"></td>
                                <td>{{$product->spec}}</td>
                                <td>
                                    @if($product->unit == 1)
                                        <span class="label label-default arrowed-in-right arrowed">盒</span>
                                    @elseif($product->unit == 2)
                                        <span class="label label-yellow arrowed-in-right arrowed">瓶</span>
                                    @elseif($product->unit == 3)
                                        <span class="label label-purple arrowed-in-right arrowed">袋</span>
                                    @else
                                        <span class="label label-primary arrowed-in-right arrowed">支</span>
                                    @endif
                                </td>
                                <td>{{$product->category['name']}}</td>
                                <td>{{$product->brand['name']}}</td>
                                <td>{{doubleval($product->price)}}元</td>
                                <td>{{doubleval($product->member_price)}}元</td>
                                <td>{{doubleval($product->purchase_price)}}元</td>
                                <td>
                                    <input type="text" class="ace stock" value="{{$product->stock}}"
                                           style="width: 40px;">
                                </td>
                                <td>{{$product->valid_at}}</td>
                                <td>
                                    <input type="text" class="ace sort_order" value="{{$product->sort_order}}"
                                           style="width: 40px;">
                                </td>
                                <td>{{$product->company}}</td>
                                <td>
                                    <span class="label label-yellow arrowed-in-right arrowed">{{$product->entry}}</span>
                                </td>
                                <td>
                                    <div class="visible-md visible-lg hidden-sm hidden-xs action-buttons">
                                        <a class="green" href="{{route('cashier.purchase.edit', $product->id)}}"><i
                                                    class="icon-pencil bigger-130"></i></a>
                                        <a class="red" href="{{route('cashier.purchase.destroy', $product->id)}}"
                                           data-method="delete" data-token="{{csrf_token()}}"
                                           data-confirm="确定要删除此条记录么?">
                                            <i class="icon-trash bigger-130"></i></a>
                                    </div>
                                </td>
                            </tr>
                        @endforeach

                        </tbody>
                    </table>
                    <div class="row">
                        <div class="col-sm-6">共 {{$purchases->total()}} 条记录</div>
                        <div class="col-sm-6">
                            <div class="dataTables_paginate paging_bootstrap">
                                <ul class="pagination">
                                    {{ $purchases->appends(Request::all())->links() }}
                                </ul>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
@endsection

@section('js')
    <script>
        $(function () {
            //ajax排序
            $('.sort_order').change(function () {
                var data = {
                    id: $(this).parents('tr').data('id'),
                    sort_order: $(this).val()
                };

                $.ajax({
                    type: 'PATCH',
                    url: '{{ route('cashier.purchase.sort_order') }}',
                    data: data,
                    success: function (data) {
                        window.location.reload();
                    }
                });

                return false;
            })


            //ajax修改库存
            $('.stock').change(function () {
                var data = {
                    id: $(this).parents('tr').data('id'),
                    stock: $(this).val()
                };

                $.ajax({
                    type: 'PATCH',
                    url: '{{ route('cashier.purchase.stock') }}',
                    data: data,
                    success: function (data) {
                        window.location.reload();
                    }
                });

                return false;
            })

            //全选，反选
            $("#check_all").click(function () {
                $(':checkbox').prop("checked", this.checked);
            });

            //多选删除
            $('.del_all').click(function () {
                var length = $(".checked_id:checked").length;
                if (length == 0) {
                    alert("您必须至少选中一条!");
                    return false;
                }

                if (confirm('确定要删除么？')) {
                    var checked_id = $(".checked_id:checked").serialize();

                    $.ajax({
                        type: "DELETE",
                        url: "{{route('cashier.purchase.delete_more')}}",
                        data: checked_id,
                        success: function () {
                            location.href = location.href;
                        }
                    });

                }
            });
        })
    </script>
@endsection
